package day05.StringBuiders;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/26
 * //2.给定一个字符串数组。按照字典顺序进行从小到大的排序。{"nba","abc","cba","zz","qq","haha"}
 * 方式一：数组中元素，自身具备比较性
 * String implements Comparable<String>接口，重写了compareTo(o)
 * Arrays.sort(arr)
 *
 * 方式二：容器具备比较性  传入Comparator比较器
 * Arrays.sort(arr , new Comparator<String>(){ ... })
 */
public class Test2 {
    public static void main(String[] args) {
        String[] arr1 = {"nba", "abc", "cba", "zz", "qq", "haha"};
        Arrays.sort(arr1);
        System.out.println("排列结果：");
        printArray(arr1);
        Arrays.sort(arr1, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });
        System.out.println("排列结果：");
        printArray(arr1);
    }

    public static void printArray(String[] arr) {
        for (String str : arr) {
            System.out.print(str + " ");
        }
        System.out.println();
   }
}
